import BaseUtil from '@/BaseUtil';
import Buff from '@/Buff';
import BuffUtil from '@/BuffUtil';
import ObjectPool from '@/ObjectPool';
import SelectUtil from '@/SelectUtil';
import { OrderId } from '@/globals/order';
import BuffCommon from 'code/Core/Common/BuffCommon';
import UnitCommon from 'code/Core/Common/UnitCommon';
import AbilityBase from 'code/Scripts/Abilities/base/AbilityBase';
/**
 * 暴风雪
 **/
export default class FS_Bilizard extends AbilityBase {
    static instance: FS_Bilizard;
    vestPool: ObjectPool<unit> = new ObjectPool<unit>(() => {
        let vest = CreateUnit(Player(0), 'ewnv', 0, 0, 0);
        UnitAddAbility(vest, 'Abfm');
        return vest;
    });
    buffType: AppBuffType = {
        id: 'buff_FS_Bilizard',
        art: this.art,
        isDebuff: true,
        name: this.abilityName,
        ubertip: '该单位攻速和移速降低了',
        interval: 1,
        onInterval: this.buffInterval.bind(this),
        stackCountMax: 10,
        dur: 5,
        attribute: {
            move_speed: -5,
            attackSpd_p: -0.05,
        },
        targetArt: 'Abilities\\Spells\\Other\\FrostDamage\\FrostDamage.mdl',
        targetAttach: 'origin',
    };
    buffInterval(buff: Buff) {
        let lv = this.getAbilityLevel(buff.getCaster());
        let dmg = ((15 + UnitCommon.取法强(buff.getCaster()) * 0.4) * lv) / 5;
        DamageSystemInstance.applyIceDamage(buff.getParent(), buff.getCaster(), dmg, this.id, true);
    }
    constructor(id: string) {
        super(id);
        FS_Bilizard.instance = this;
        this.init();
        se.onUnitSpellEffect(this.onSpellEffect.bind(this), this.id);
    }
    init() {
        BuffUtil.registerBuffType(this.buffType);
    }
    cast<T extends Object = any>(tarX: number, tarY: number, source: unit, target: unit, data?: T): void {
        let vest = this.getAnVest(source, tarX, tarY);
        IssuePointOrderById(vest, OrderId.Blizzard, tarX, tarY);
        BaseUtil.runLater(10, () => this.vestPool.returnObject(vest));
        BaseUtil.onTimer(1, (c) => {
            let lv = this.getAbilityLevel(source);
            let es = SelectUtil.getEnemyUnitsInRange(source, 250 + 50 * lv, tarX, tarY);
            let dmg = (15 + UnitCommon.取法强(source) * 0.25) * lv;
            for (let e of es) {
                // let buff = BuffUtil.addBuff(e, this.buffType.id, source);
                let buff = BuffCommon.addBuff({ target: e, caster: source, buffTypeId: this.buffType.id, ablityId: this.id });
                DamageSystemInstance.applyIceDamage(e, source, dmg, this.id);
                if (buff.stackCount >= 5) {
                    StunSystemIntance.stun(e, 2.5, source, '冰冻');
                }
            }
            return c < 6;
        });
    }
    onSpellEffect() {
        this.cast(GetSpellTargetX(), GetSpellTargetY(), GetTriggerUnit(), GetSpellTargetUnit());
    }
    getAnVest(o: unit, x: number, y: number) {
        let v = this.vestPool.borrowObject();
        SetUnitLifePercentBJ(v, 100);
        SetUnitManaPercentBJ(v, 100);
        SetUnitOwner(v, GetOwningPlayer(o), false);
        SetUnitAbilityLevel(v, 'Ahym', GetUnitAbilityLevel(o, this.id));
        if (x && y) SetUnitPosition(v, x, y);
        return v;
    }
}
